package com.macro.mall.portal.service.impl;

import com.macro.mall.common.service.RedisService;
import com.macro.mall.mapper.UmsStoreMapper;
import com.macro.mall.model.*;
import com.macro.mall.portal.service.UmsStoreCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


/**
 * UmsAdminCacheService实现类
 * Created by macro on 2020/3/13.
 */
@Service
public class UmsStoreCacheServiceImpl implements UmsStoreCacheService {
    @Autowired
    private RedisService redisService;
    @Value("${redis.database}")
    private String REDIS_DATABASE;
    @Value("${redis.expire.common}")
    private Long REDIS_EXPIRE;
    @Value("${redis.key.store}")
    private String REDIS_KEY_STORE;
    @Autowired
    private UmsStoreMapper umsStoreMapper;

    @Override
    public void delStore(Long storeId) {
        String key = REDIS_DATABASE +":" +REDIS_KEY_STORE+ ":"+storeId;
        this.redisService.del(key);
    }


    @Override
    public void setStore(UmsStore store) {
        String key = REDIS_DATABASE +":" +REDIS_KEY_STORE+ ":"+store.getId();
        this.redisService.set(key,store,REDIS_EXPIRE);
    }

    @Override
    public UmsStore getStore(Long storeId) {
        String key = REDIS_DATABASE +":" +REDIS_KEY_STORE+ ":"+storeId;
        UmsStore store = (UmsStore)this.redisService.get(key);
        if(store==null){
            store = this.umsStoreMapper.selectByPrimaryKey(storeId);
            this.setStore(store);
        }
        return  store;
    }
}
